//从一个给定的字符串中寻找重复次数最多的字符以及重复出现的次数。如有多个重复的都求出
package collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class General_ex {

	public static void main(String args[]) {
		doString("jeovjrggwfgriufWERaioezzigqwhjkuewmhqgzycewttyufgwqjkfaejgvhbfoijfagrjnmcxvewsdf");
	}
	public static void doString (String inputStr) {
		//char[] chars = inputStr.toCharArray();

		ArrayList lists = new ArrayList();
		Set set = new TreeSet();
		//TreeSet set = new TreeSet();
		for (int i = 0;i < inputStr.length(); i++) {
			lists.add(inputStr.charAt(i));
			set.add(inputStr.charAt(i));
		}
		/*
		for (int i = 0; i < chars.length; i++) {
			lists.add(chars[i]);
			set.add(chars[i]);
		}*/
		Collections.sort(lists);
		System.out.println(lists);
		System.out.println(set);
		
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < lists.size(); i++) {
			sb.append(lists.get(i));
		}
		inputStr = sb.toString();
		System.out.println(inputStr);
		
		int max = 0;
		String maxString = "";
		ArrayList maxList = new ArrayList();
		
		for (Iterator iter = set.iterator(); iter.hasNext();) {
			String s = iter.next().toString();
			int begin = inputStr.indexOf(s);
			int end = inputStr.lastIndexOf(s);
			int count = end - begin + 1;
			
			if(count > max) {
				max = count;
				maxString = s;
				maxList.add(s);
			}
			else if(count == max) {
				maxList.add(s);
			}
		}
		int index = 0;
		for (int i = 0; i < maxList.size(); i++) {
			if (maxList.get(i).equals(maxString)) {
				index = i;
				break;
			}
		}
		System.out.print("出现最多的char: ");
		for (int i = index; i < maxList.size(); i++) {
			System.out.print(maxList.get(i)+" ");
		}
		System.out.println("次数： "+max);
	}
}
